#!/usr/bin/php

<?php
include_once(dirname(__FILE__).'/config.php');

$store = ARC2::getStore($arc_config);

$URLPrefix = "http://desanti.org/";

$insertQueryPrefix =  "PREFIX cols: <" . $URLPrefix . "schemas/collections#>\n";
$insertQueryPrefix .= "INSERT INTO <" . $URLPrefix . "> {\n";

$root = (dirname(__FILE__).'/');
$collectionsDirName = 'collections/';

function id($file){
  global $root, $URLPrefix;
  $id = substr_replace($file, $URLPrefix, 0, strlen($root));
  return $id;
}

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root.$collectionsDirName), RecursiveIteratorIterator::CHILD_FIRST);

foreach($iterator as $file){
  $filename = $file->getFilename();
  if($file->isDir() && strpos($filename,'.')!==0){
    if(!strpos($filename,'_')){ 
      if($filename < 200){
        $q = $insertQueryPrefix . '<' . id($file) . '> rdf:type cols:Cahier . ' . "\n"; 
      }else{
        $q = $insertQueryPrefix . '<' . id($file) . '> rdf:type cols:Pochette . ' . "\n"; 
      }
    }else{
      $q = $insertQueryPrefix . '<' . id($file) . '> rdf:type cols:Insert . ' . "\n"; 
      $insertedAt = $file->getPath() . '/' . sprintf("%04d", $file->getFilename());
      $q .= '<' . id($file) . '> cols:insertedAt ' . '<' . id($insertedAt) . '> . ' . "\n";
      $parent = substr(id($file), 0, strrpos(id($file), '/'));
      $q .= '<' . $parent . '> cols:parentOf <' . id($file) . '> . ' . "\n";
      $q .= '<' . id($file) . '> cols:childOf <' . $parent . '> . ' . "\n";
    }

    $collectionLabel = substr_replace(id($file), '', 0, strlen($URLPrefix . $collectionsDirName));

    $q .= '<' . id($file) . '> rdfs:label "' . $collectionLabel . '" . ' . "\n";

    $pages = '<' . id($file) . '/pages>';
    $q .= '<' . id($file) . '> cols:pages ' . $pages . ' . ' . "\n";
    $q .= $pages . ' rdf:type rdf:Seq . ' . "\n";

    $subiterator = new DirectoryIterator($file);
    foreach($subiterator as $it){
      if($it->isFile() && strpos($it, '_high.jpeg')){
	$absoluteFilename = $it->getRealPath();
	$page = substr($absoluteFilename, 0, strpos($absoluteFilename, '_high.jpeg')); 
	$thumbnail = $page . '_low.jpeg';
	$imgId = id($absoluteFilename);
	$pageId = id($page);
	$thumbnailId = id($thumbnail);

        $q .= '<' . $pageId . '> rdf:type cols:Page .' . "\n";
	$q .= $pages . ' rdf:_' . (int) $it->getFileName() . ' <' . $pageId . "> . \n";
        $q .= '<' . $pageId . '> cols:collection <' . id($file) .'> .' . "\n";
	$q .= '<' . $pageId . '> cols:image <' . $imgId . '> .' . "\n";
	$q .= '<' . $pageId . '> cols:thumbnail <' . $thumbnailId . '> .' . "\n";
	$q .= '<' . $imgId . '> rdf:type cols:Image .' . "\n";
	$q .= '<' . $thumbnailId . '> rdf:type cols:Image .' . "\n";

        $imgSize = getimagesize($absoluteFilename);
        $thumbSize = getimagesize($thumbnail);

        $q .= '<' . $imgId . '> cols:hasWidth "' . $imgSize[0] . '" .' . "\n";
        $q .= '<' . $imgId . '> cols:hasHeight "' . $imgSize[1] . '" .' . "\n";
        $q .= '<' . $thumbnailId . '> cols:hasWidth "' . $thumbSize[0] . '" .' . "\n";
        $q .= '<' . $thumbnailId . '> cols:hasHeight "' . $thumbSize[1] . '" .' . "\n";
      }
    }

    $q .= "}\n";
    //print $file;
    print $q;
    print "\n\n\n\n";
    // $store->query($q);
    // if($errors = $store->getErrors()){
    //   print 'requete en cause ';
    //   print $q;
    //   print join("\n", $errors);
    //   exit (10);
    // }
  }
}
?>
